#import "OmPersonIncomView.h"
@interface OmPersonIncomView ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIView *_contentView;
    UIView *_dismissView;
    NSInteger _currentIndex;
    NSArray *_titleArr;
}
@property (strong, nonatomic) UIPickerView *pickerView;
@end
@implementation OmPersonIncomView
- (instancetype)init {
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _currentIndex = 2;
        [self _layoutMainView];
    }
    return self;
}
+ (OmPersonIncomView *)showInViewWithBlock:(IncomeBlock)selectBlock {
    OmPersonIncomView *alert = [[OmPersonIncomView alloc]init];
    alert.selectBlock = selectBlock;
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
    [alert showInView];
    return alert;
}
- (void)cancelHandler {
    [self hiddenInView];
}
- (void)sureHandler {
    if (self.selectBlock) {
        NSString *title = [_titleArr objectAtIndex:_currentIndex];
        self.selectBlock(title);
    }
    [self hiddenInView];
}
- (void)showInView {
    self.hidden = NO;
    _contentView.transform = CGAffineTransformMakeScale(0.5, 0.5);
    [UIView animateWithDuration:0.05
                     animations:^{
                         self->_dismissView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
                         self->_contentView.transform = CGAffineTransformMakeScale(1.0, 1.0);
                     } completion:^(BOOL finished) {
                     }];
}
- (void)hiddenInView {
    [UIView animateWithDuration:0.2 animations:^{
        self->_dismissView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        self->_contentView.transform = CGAffineTransformMakeScale(0.5, 0.5);
    }completion:^(BOOL finished) {
        self.hidden = YES;
        [self removeFromSuperview];
    }];
}
#pragma mark - delete
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _titleArr.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 38.0f;
}
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 280;
}
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *str = [_titleArr objectAtIndex:row];
    NSMutableAttributedString *AttributedString = [[NSMutableAttributedString alloc]initWithString:str];
    [AttributedString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20], NSForegroundColorAttributeName:HexadecimalColor(@"#333333")} range:NSMakeRange(0, [AttributedString  length])];
    return AttributedString;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _currentIndex = row;
}
- (void)_layoutMainView {
    _titleArr = @[@"< 2000$",@"2000$ - 4000$",@"4000$ - 6000$",@"6000$ - 8000$",@"> 8000$"];
    self.backgroundColor = [UIColor clearColor];
    _dismissView = [[UIView alloc]init];
    [self addSubview:_dismissView];
    _dismissView.frame = self.bounds;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelHandler)];
    [_dismissView addGestureRecognizer:tap];
    _contentView = [[UIView alloc]init];
    _contentView.backgroundColor = [UIColor whiteColor];
    ViewRadius(_contentView, 6);
    _contentView.layer.masksToBounds = YES;
    [self addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(280, 330));
    }];
    [self addHeaderView];
    [self addContentView];
    [self addFootView];
}
- (void)addContentView {
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 66, 280, 185)];
    self.pickerView.backgroundColor = [UIColor whiteColor];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [_contentView addSubview:self.pickerView];
    [self.pickerView selectRow:_currentIndex inComponent:0 animated:NO];
}
- (void)addFootView {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom].fp_addTargetActionForControlEvents(self,@selector(sureHandler),UIControlEventTouchUpInside);
    [btn setTitle:ASLocalizedString(@"确定") forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:HexadecimalColor(@"#F07896")];
    ViewRadius(btn, 22);
    [_contentView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->_contentView);
        make.bottom.equalTo(self->_contentView).with.offset(-23);
        make.size.mas_equalTo(CGSizeMake(150, 44));
    }];
}
- (void)addHeaderView {
    UILabel *label = [[UILabel alloc]init].fp_font([UIFont systemFontOfSize:15 weight:UIFontWeightMedium]).fp_text(ASLocalizedString(@"请选择收入(每月)")).fp_textColor([UIColor blackColor]);
    [_contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_contentView).with.offset(23);
        make.top.equalTo(self->_contentView).with.offset(25);
    }];
    UIView *lineView = [[UIView alloc]init].fp_backgroundColor(HexadecimalColor(@"#E6E6E6"));
    [_contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label);
        make.top.equalTo(self->_contentView).with.offset(55);
        make.right.equalTo(self->_contentView).with.offset(18);
        make.height.mas_equalTo(1);
    }];
}
@end
